const loginBox = document.querySelector('.login')
const regBox = document.querySelector('.register')
document.querySelector('button.login').addEventListener('click', async e => {
    e.preventDefault()
    const username = document.querySelector('.login [name=username]').value
    const password = document.querySelector('.login [name=password]').value
    if (username.length < 2 || username.length > 15) {
        return Toast.info('用户名长度必须为2~15位')
    }
    if (password.length < 6 || password.length > 15) {
        return Toast.info('密码长度必须为6~15位')
    }
    const res = await axios({
        url: 'http://www.itcbc.com:8000/api/login',
        method: 'POST',
        data: {
            username, password
        }
    })
    if (!res.data.code) {
        Toast.success(res.data.message)
        localStorage.setItem('token', res.data.token)
        localStorage.setItem('username', username)
        setTimeout(() => {
            location.href = './index.html'
        }, 1500)
    } else {
        Toast.fail(res.data.message)
    }
})
document.querySelector('button.register').addEventListener('click', async e => {
    e.preventDefault()
    const username = document.querySelector('.register [name=username]').value
    const password = document.querySelector('.register [name=password]').value
    if (username.length < 2 || username.length > 15) {
        return Toast.info('用户名长度必须为2~15位')
    }
    if (password.length < 6 || password.length > 15) {
        return Toast.info('密码长度必须为6~15位')
    }
    const res = await axios({
        url: 'http://www.itcbc.com:8000/api/register',
        method: 'POST',
        data: {
            username, password
        }
    })
    if (!res.data.code) {
        Toast.success(res.data.message)
        loginBox.style.display = 'block'
        regBox.style.display = 'none'
        document.querySelector('.login [name=username]').value = username
        document.querySelector('.login [name=password]').value = password
    } else {
        Toast.fail(res.data.message)
    }
})




document.querySelector('.goRegister').addEventListener('click', () => {
    loginBox.style.display = 'none'
    regBox.style.display = 'block'
})
document.querySelector('.goLogin').addEventListener('click', () => {
    loginBox.style.display = 'block'
    regBox.style.display = 'none'
})